package com.minhua.jijiehao2.http;

import com.google.gson.JsonParseException;
import com.minhua.jijiehao2.bean.BaseBean;
import com.minhua.jijiehao2.utils.LoadingDialogUtil;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;

import java.net.UnknownHostException;
import java.text.ParseException;

import com.minhua.jijiehao2.constants.HttpConst;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


/**
 * @author moo
 */
public abstract class BaseSubscriber<T> implements Observer<BaseBean<T>> {
    private Disposable d;
    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;


    @Override
    public void onError(Throwable e) {
        LoadingDialogUtil.INSTANCE.dismissDialog();
        DisposableController.INSTANCE.remove(d);

        Throwable throwable = e;
        //获取最根源的异常
        while (throwable.getCause() != null) {
            e = throwable;
            throwable = throwable.getCause();
        }
        if (e instanceof HttpException) {
            //HTTP错误
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                    com.minhua.jijiehao2.utils.T.showShort("暂无权限！");
                    break;
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    com.minhua.jijiehao2.utils.T.showShort("网络异常！");
                    break;
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            com.minhua.jijiehao2.utils.T.showShort("解析错误！");
        } else if (e instanceof UnknownHostException) {
            com.minhua.jijiehao2.utils.T.showShort("网络未连接！");
        }
        onComplete();
    }

    @Override
    public void onNext(BaseBean<T> tBaseBean) {
        int retCode = tBaseBean.getCode();
        String retMsg = tBaseBean.getMsg();
        if (retCode == HttpConst.SUCCESS_STATUS) {
            dealData(tBaseBean);
        } else {
            onFail(retCode, retMsg);
        }
    }

    /**
     * 成功回调
     *
     * @param t 返回数据
     */
    protected void dealData(@NotNull BaseBean<T> t) {
        LoadingDialogUtil.INSTANCE.dismissDialog();
    }

    /**
     * 失败回调(可选重写)
     *
     * @param code 返回码
     * @param msg  返回msg
     */
    protected void onFail(int code, String msg) {
        LoadingDialogUtil.INSTANCE.dismissDialog();
        com.minhua.jijiehao2.utils.T.showShort(msg);
    }

    @Override
    public void onSubscribe(Disposable d) {
        this.d = d;
        DisposableController.INSTANCE.add(d);
    }

    @Override
    public void onComplete() {
        DisposableController.INSTANCE.remove(d);
    }

    public void cancel() {
        if (d != null && !d.isDisposed()) {
            d.dispose();
            DisposableController.INSTANCE.remove(d);
        }
    }
}
